package dao;

import DBU.DBUtil;
import entity.Flower;
import entity.FlowerStore;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class FlowerStoreDAOImp implements FlowerStoreDAO{
    @Override
    public List<FlowerStore> checkFlowerStore() {
        Connection conn = DBUtil.getConnection();
        String sql = "select * from flower";
        PreparedStatement ps = null;
        try {
            ps  = conn.prepareStatement(sql);
            ps.executeQuery();
            return (List<FlowerStore>) ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean buyFlower(int fid,int num) {
        double price = 0;    //单价
        double sum = 0;     //总价
        Connection conn = DBUtil.getConnection();
        //查询价格，计算总价
        String checksql = "select price from flower where fid = "+fid;
        ResultSet rs = null;
        try {
            PreparedStatement ps1 = conn.prepareStatement(checksql);
            rs = ps1.executeQuery();
            while (rs.next()){
                price = rs.getDouble("price");
            }
            sum  = price*num;
            //更新销量以及销售额数据
            String updatesql = "update flowerstore set acount = acount+"+num+",amount = amount+"+sum;
            PreparedStatement ps2 = conn.prepareStatement(updatesql);
            ps2.executeQuery();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //更新销量以及销售额
        return false;
    }

    @Override
    public boolean addFlower() {
        Connection conn = DBUtil.getConnection();
        String sql = "update flowerstore set varnum = varnum + 1";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
